<template>
  <div>

  </div>
</template>

<script lang="ts">
import { ref, defineComponent, getCurrentInstance,onMounted } from 'vue'
export default defineComponent({
  name: 'Curry',
  setup: () => {
    let {ctx}:any = getCurrentInstance();

    onMounted(() => {
        let result = doFnCurry();
        console.log(result);
        let presult = partialFnCurry();
        console.log(presult);
        let pr = partialRightFnCurry();
        console.log(pr);
        useWithFnCurry();
        complementFnCurry();
        memoizeWithFnCurry();
    })

    const doFnCurry = () =>{
        console.log("将一个函数柯里化");
        const addNums = (a:number,b:number,c:number,d:number) => a+b+c+d;
        const curriedAddNums = ctx.$R.curry(addNums);
        let f = curriedAddNums(1,2);
        let g = f(3);
        let e = g(4);
        return e;
    }

    const partialFnCurry = () =>{
        let greet = (zhu:string,wei:string,bin:string,hao:string) =>{
            let str = `${zhu} => ${wei} => ${bin} => ${hao}`;
            return str;
        }
        let setZhuWei = ctx.$R.partial(greet,['梁子明','吃']);
        let setBin = ctx.$R.partial(setZhuWei,['苹果']);
        let greetStr = setBin("!");
        // 梁子明 => 吃 => 苹果 => !
        return greetStr;
    }

    const partialRightFnCurry = () =>{
        let greet = (zhu:string,wei:string,bin:string,hao:string) =>{
            let str = `${zhu} => ${wei} => ${bin} => ${hao}`;
            return str;
        }
        let setWeiBinHao = ctx.$R.partialRight(greet,['吃','龙虾',"~"]);
        let greetStr = setWeiBinHao("梁大胖");
        // 梁大胖 => 吃 => 龙虾 => ~
        return greetStr;
    }

    const useWithFnCurry = () =>{
        const decreaseOne = (x:number) =>x-1;
        const increaseOne = (x:number) =>x+1;
        ctx.$R.useWith(Math.pow,[decreaseOne,increaseOne])(3)(4);
        // 32 
        // 先算出 decreaseOne 3-1=2
        // 在算出 increaseOne 4+1 = 5
        //Math.pow，2的5次方 Math.pow(2,5)

    }

    const complementFnCurry = () =>{
        console.log("complement返回一个新函数,如果原函数返回true,该函数返回false,如果原函数返回false,该函数返回true");
        const gt10 = (num:number) => num>10;
        const lte10 = ctx.$R.complement(gt10);
        lte10(gt10(7));
    }

    const memoizeWithFnCurry = () =>{
        console.log("memoizeWithFnCurry");
        let count = 0;

        const factorial = ctx.$R.memoizeWith(ctx.$R.identity, (n:number) => {
            count += 1;
            return ctx.$R.product(ctx.$R.range(1, n + 1));
        });

        let re1 = factorial(5); //=> 120
        let re2 = factorial(5); //=> 120
        let re3 = factorial(5); //=> 120
        console.log(re1,re2,re3);
        let re4 = factorial(6);
        console.log(re4);
        let re5 = factorial(5); //=> 120
        console.log(re5);
        count; //=> 1
    }

    return { doFnCurry,partialFnCurry,useWithFnCurry,complementFnCurry,memoizeWithFnCurry }
  }
})
</script>

<style scoped>
a {
  color: #42b983;
}

label {
  margin: 0 0.5em;
  font-weight: bold;
}

code {
  background-color: #eee;
  padding: 2px 4px;
  border-radius: 4px;
  color: #304455;
}
</style>
